
local LuaMongo = _G.lua_mongo
local DB = require("common.DB")
local RankDefine = require("rank.RankDefine")
local Msg = require("core.Msg")
local HorseConfig = require("excel.Horse")
local StageConfig = HorseConfig.stage
local Log = require("common.Log")
local PfLogic = require("tencentApi.PfLogic")

HORSE_RANK = HORSE_RANK or {}
--HORSE_RANK = {index = {uuid,name, horseLv, horseName, horsePower,create = os.time()}}

function setRankList(human, list, index)
  list.type = RankDefine.HORSE_RANK_TYPE
  list.name = HORSE_RANK[index].name
  list.uuid = HORSE_RANK[index].uuid
  list.horseLv = HORSE_RANK[index].horseLv
  list.horseName = HORSE_RANK[index].horseName
  list.horsePower = HORSE_RANK[index].horsePower
  if PfLogic.isBlueShow(human) then
    list.blue = HORSE_RANK[index].blue
  else
    list.blue = "0000"
  end
  list.args = 0
end

function GetRankList(human, sendMsg)
  sendMsg.horselist[0] = #HORSE_RANK
  for i=1, #HORSE_RANK do
    if HORSE_RANK[i].uuid == human._id then
      sendMsg.myRank = i
    end
    setRankList(human, sendMsg.horselist[i], i)
  end 
  Msg.send(sendMsg, human.fd)
end

function getFirst(human, list)  
  list[0] = 0 
  if HORSE_RANK[1] then
    list[0] = list[0] + 1
    setRankList(human, list[list[0]], 1)
  end
end

function getHorseLv(human)
  local starLv = human.db.horseStarLv
  local lv = math.floor(starLv / 10) + 1
  local stage = human.db.horseStageLv
  if math.floor(starLv/100) == stage then
    lv = lv - 1
  end
  return lv
end

function getHorseName(human)
  return StageConfig[human.db.horseStageLv].name
end

function setRankInfo(human, index, horsePower)
  HORSE_RANK[index] = {}
  HORSE_RANK[index].uuid = human._id
  HORSE_RANK[index].horseLv = getHorseLv(human)
  HORSE_RANK[index].name = human.name
  HORSE_RANK[index].horseName = getHorseName(human)
  HORSE_RANK[index].horsePower = horsePower
  HORSE_RANK[index].account = human.account
  HORSE_RANK[index].create = os.time()
  HORSE_RANK[index].blue = string.format("%d%d%d%d",human.db.blueVipLv, human.db.blueYearVip, human.db.blueSuperVip, human.db.level3366)
end

function sortFunc(a, b)
  if a.horsePower == b.horsePower then
    return a.create < b.create
  end
  return a.horsePower > b.horsePower
end

function isInRank(human, horsePower)
  for i=1, #HORSE_RANK do
    if HORSE_RANK[i].uuid == human._id then
      if horsePower ~= HORSE_RANK[i].horsePower then
        setRankInfo(human, i, horsePower)
        table.sort(HORSE_RANK, sortFunc)
      end
      return true
    end
  end
  return false
end

--外部调用，坐骑战力变更的时候调用
function horsePowerChange(human, horsePower)
  if human.db.lv < 35 then return end
  if isInRank(human, horsePower) then
    return
  end
  if #HORSE_RANK < RankDefine.RANK_MAX_CNT then
    setRankInfo(human, #HORSE_RANK + 1, horsePower)
  elseif #HORSE_RANK >= RankDefine.RANK_MAX_CNT and horsePower > HORSE_RANK[#HORSE_RANK].horsePower then
    setRankInfo(human, RankDefine.RANK_MAX_CNT, horsePower)
  else
    return false
  end
  table.sort(HORSE_RANK, sortFunc)
end

function getRank()
  local dbType = {type = RankDefine.HORSE_RANK_TYPE}
  LuaMongo.find(DB.db_rank, dbType)
  while true do
    local tmp = {}
    if not LuaMongo.next(tmp) then
      break
    end
    HORSE_RANK[tmp.index] = {}
    HORSE_RANK[tmp.index].uuid = tmp.uuid
    HORSE_RANK[tmp.index].horseLv = tmp.horseLv
    HORSE_RANK[tmp.index].name = tmp.name
    HORSE_RANK[tmp.index].horseName = tmp.horseName
    HORSE_RANK[tmp.index].horsePower = tmp.horsePower
    HORSE_RANK[tmp.index].account = tmp.account
    HORSE_RANK[tmp.index].create = tmp.create or os.time()
    HORSE_RANK[tmp.index].blue = tmp.blue or "0000"
  end
end

function wrapperHorseData(index)
  local db = {}
  db.type = RankDefine.HORSE_RANK_TYPE
  db.index = index
  db.uuid = HORSE_RANK[index].uuid
  db.horseLv = HORSE_RANK[index].horseLv
  db.name = HORSE_RANK[index].name
  db.horseName = HORSE_RANK[index].horseName
  db.horsePower = HORSE_RANK[index].horsePower
  db.account = HORSE_RANK[index].account
  db.create = HORSE_RANK[index].create or os.time()
  db.blue = HORSE_RANK[index].blue or "0000"
  return db
end

function saveRank()
  for i=1, #HORSE_RANK do
    local db = wrapperHorseData(i)
    LuaMongo.insert(DB.db_rank, db)
  end
end

function writeLog()
  for i=1, #HORSE_RANK do
    Log.write(Log.LOGID_OSS_HORSE_RANK, HORSE_RANK[i].uuid, HORSE_RANK[i].account, HORSE_RANK[i].name, HORSE_RANK[i].horsePower, i)
  end
end